!pr1
Strange Way to Divide by 7.................Bob Sander-Cederlof

Division by seven is a necessary step for hi-res plotting routines.  The quotient is the byte index on a given scan line.  The remainder gives the bit position within that byte.

The hi-res code inside the Applesoft ROMs uses a subtraction loop to divide by seven, which can loop up to 36 times at 7 cycles per loop.  This is a maximum of over 250 cycles, which is why super-fast hi-res usually uses lookup tables for the quotient and remainder.

I stumbled on a faster way of dividing any value up to 255 by seven.  This is not directly usable by standard hi-res, because the x-coordinate can be as large as 279.  My trick also does not give the remainder, just the quotient.

Here is the program, along with a test routine which tries every value from 0 to $FF, printing the quotient.  The output from the test program is also shown, and you can see that the quotient is correct in every case.  Can you explain why it works?

[ Hint:  1/7 = 1/8 + 1/64 + 1/512 + 1/4096 + ... ]



<<<<program>>>>



It is possible to divide by 3 or 15 using a program based on the same principle as the divide-by-seven above.  Here is the code for those.



>>>>listings>>>>>, side by side>>>>>



Using the divide by 15, you could make a divide by ten.  First multiply the original number by three (by shifting one bit left and adding), then divide by 15 using the above program, and then by 2 (by shifting one bit right).  Since 3X/30 = X/10, there you have it.
